package cn.tedu;

import cn.tedu.dao.DeptDao;
import cn.tedu.pojo.Dept;
import cn.tedu.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.platform.engine.support.hierarchical.ThrowableCollector;


import java.io.IOException;
import java.io.InputStream;
import java.util.List;


public class mybatis {
    SqlSessionFactory factory;
    @BeforeEach//在@Test执行前执行
    public void inte() throws IOException {
        InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
        factory = new SqlSessionFactoryBuilder().build(in);
    }
    @Test
    public void getById(){
        SqlSession sqlSession = factory.openSession();
        DeptDao dao = sqlSession.getMapper(DeptDao.class);
        Dept d = dao.getById();
        System.out.println(d);
    }

    @Test
    public void inter() throws IOException {
        SqlSession session = factory.openSession();
        DeptDao dao = session.getMapper(DeptDao.class);
        List<Dept> list = dao.getAll();
        for(Dept d:list){
            System.out.println(d);
        }
    }

    //测试xml的方式开始
    @Test
    public void getAll() throws IOException {
        SqlSession session = factory.openSession();
        List<User>list = session.selectList("userMapperNS.getAll");
        for(User u:list){
            System.out.println(u);
        }
        User u = session.selectOne("userMapperNS.getAll1");
        System.out.println(u);
    }
}
